//=========================================================================
//  Apache License, Version 2.0
//  Notes:Use this command to anonymously check out the latest project source code:
//  Non-members may check out a read-only working copy anonymously over HTTP.
//  SVN checkout http://YHHLineCommandWsp.googlecode.com/svn/trunk/ 
//
//  Author:		Kevin Yang
//  Email:		Bigearrabbit@qq.com
//
//  DATE:		2013-4-6
//
//  File Name:	YHHELineCommandFactory.cpp
//
//  Notes:
//  Provide implementation to interface YHHILineCommandFactory
//
//============================================================================


// Local Framework
#include "YHHELineCommandFactory.h"
//YHHCatiaBaseUtlFrm Framework
#include "YHHCatiaBaseDefine.h"

// YHHLineCommandInterfaces Framework
#include "YHHILineCommandChild.h" // needed by the factory to return a pointer on this interface

// ObjectSpecsModeler Framework
#include "CATISpecObject.h"      // needed to manage features
#include "CATICatalog.h"         // needed to retrieve the Line Command feature startup

// ObjectModelerBase Framework
#include "CATIContainer.h"       // 

// ObjectSpecsModeler Framework
#include "CATCatalogFactoryServices.h" // needed to load the Line Command catalog

// AlgorithmConfiguration Management
#include "CATMmrAlgoConfigServices.h"  //needed to subscribe to repository for AlgorithmConfiguration

//BackUp / StartUp Management
#include "CATIInputDescription.h"
#include "CATMmrFeatureAttributes.h"

// System
#include "CATUnicodeString.h"

CATImplementClass ( YHHELineCommandFactory ,
				   DataExtension         ,
				   CATBaseUnknown        ,
				   CATPrtCont            );

//---------------------------------------------------------------------------------------------------
// YHHELineCommandFactory : constructor
//---------------------------------------------------------------------------------------------------
YHHELineCommandFactory::YHHELineCommandFactory():
CATBaseUnknown()
{

}

//---------------------------------------------------------------------------------------------------
// YHHELineCommandFactory : destructor
//---------------------------------------------------------------------------------------------------
YHHELineCommandFactory::~YHHELineCommandFactory()
{

}
// 
// To declare that GSMTool implements YHHILineCommandFactory, insert 
// the following line in the interface dictionary:
//
// GSMTool  YHHILineCommandFactory    libYHHLineCommandChild
// Tie the implementation to its interface
// ---------------------------------------
#include "TIE_YHHILineCommandFactory.h" // needed to tie the implementation to its interface
TIE_YHHILineCommandFactory( YHHELineCommandFactory);


//---------------------------------------------------------------------------------------------------
 HRESULT YHHELineCommandFactory::CreateYHHILineCommand(YHHILineCommandChild ** oppLineCommand,
	 const YHHLineCommandChildParam * ipiValue)  
{

	//check input:
	if (NULL == oppLineCommand) return E_INVALIDARG;//error
	*oppLineCommand = NULL;//set null

	//===============================================================================================
	//
	//  What does the factory do ?
	//
	//     o -1- Retrieves a CATIContainer interface pointer on this (CATPrtCont)
	//     o -2- Opens the Line Command catalog
	//     o -3- Retrieves Line Command's startup
	//     o -4- Creates a Line Command instance
	//     o -5- Subscribes to repository for Configuration Data Storage
	//     o -6- Gets Feature Type Information for BackUp / StartUp management  
	//     o -7- Sets default values for the attributes of the instance
	//
	//
	//===============================================================================================

	//===============================================================================================
	//
	// -1- Retrieves a CATIContainer interface pointer on this (CATPrtCont)
	//
	//===============================================================================================

	CATIContainer *piContainerOnThis = NULL;
	HRESULT rc = QueryInterface( IID_CATIContainer , ( void**) &piContainerOnThis );
	if ( FAILED(rc) )
	{
		return rc;
	}

	//===============================================================================================
	//
	// -2- Opens the Line Command catalog
	//
	//===============================================================================================

	CATICatalog * piLineCommandCatalog = NULL;
	CATUnicodeString StorageName = "YHHLineCommandCatalog.CATfct";
	CATUnicodeString ClientId = "YHHLineCommandID";
	rc = ::AccessCatalog( &StorageName, &ClientId, piContainerOnThis, &piLineCommandCatalog);
	if ( FAILED(rc) )  return rc;

	//===============================================================================================
	//
	// -3- Retrieves Line Command's startup
	//
	//===============================================================================================

	CATBaseUnknown *pLineCommandStartup = NULL;
	CATUnicodeString StartupType = "YHHLineCommandChild";

	rc = piLineCommandCatalog->RetrieveSU( &pLineCommandStartup , &StartupType , "CATISpecObject" );
	if ( FAILED(rc) )  
		return rc;

	piLineCommandCatalog->Release();
	piLineCommandCatalog = NULL ;

	CATISpecObject *piSpecOnLineCommandStartup = NULL;
	rc = pLineCommandStartup->QueryInterface( IID_CATISpecObject , ( void**) &piSpecOnLineCommandStartup );
	pLineCommandStartup->Release();
	pLineCommandStartup = NULL ;

	if( FAILED(rc) ) 
		return rc;

	//===============================================================================================
	//
	// -4- Creates a Line Command instance
	//
	//===============================================================================================

	CATISpecObject * piSpecOnLineCommand = NULL;//Define CATISpecObject
	piSpecOnLineCommand = piSpecOnLineCommandStartup->Instanciate( NULL_string , piContainerOnThis );
	YHHRelease(piSpecOnLineCommandStartup);//Release

	piContainerOnThis->Release();
	piContainerOnThis = NULL ;

	//4.1 On Tree
	//DO NOT EDIT IN THE CONTROL CODE OF "KEVIN CAA WIZARD SECTION"
	//START KEVIN CAA WIZARD SECTION YHHLineCommandChild FACTRY ON TREE
	//END KEVIN CAA WIZARD SECTION YHHLineCommandChild FACTRY ON TREE

	//===============================================================================================
	//
	// -5- Subscribes to repository for Configuration Data Storage
	//
	//===============================================================================================

	rc = CATMmrAlgoConfigServices::CreateConfigurationData(piSpecOnLineCommand);
	if( FAILED(rc) )
		return rc;
	//===============================================================================================
	//
	// -6- Gets Feature Type Information for BackUp / StartUp management
	//
	//===============================================================================================

	CATIInputDescription* pInputDescriptionOnLineCommand = NULL;
	rc = (piSpecOnLineCommand)->QueryInterface( IID_CATIInputDescription, (void**) &pInputDescriptionOnLineCommand);
	if(FAILED(rc))	return rc;

	CATIInputDescription::FeatureType Feature_type = CATIInputDescription::FeatureType_Unset;

	rc = pInputDescriptionOnLineCommand -> GetFeatureType(Feature_type);
	if(FAILED(rc))
	{
		YHHRelease(pInputDescriptionOnLineCommand);//Release
		return rc;
	}

	rc = CATMmrFeatureAttributes::SetFeatureType(piSpecOnLineCommand, Feature_type);
	YHHRelease(pInputDescriptionOnLineCommand);//Release
	if(FAILED(rc))return rc;//error

	//===============================================================================================
	//
	// -7- Sets default values for the attributes of the instance
	//
	//===============================================================================================

	rc = piSpecOnLineCommand->QueryInterface( IID_YHHILineCommandChild, (void**) oppLineCommand );
	if( FAILED(rc) ) return rc;

	YHHLineCommandChildParam defaultValue;//DefaultValueParam
	return (*oppLineCommand)->SetParams(defaultValue);//Set default value
}
